package com.grgbanking.it.system.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grgbanking.it.common.entity.PageQuery;
import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.system.entity.WXMenu;
import com.grgbanking.it.system.service.IWXMenuService;

/**
 * 微信菜单管理
 * 
 * RoleController
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2014-12-18 上午9:37:21
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/system")
public class WXMenuController {
	
	@Autowired
	private IWXMenuService menuService;
	
	@ResponseBody  
	@RequestMapping(value="menu",method=RequestMethod.POST)
    public Object save(HttpSession httpSession,WXMenu menu){  
		String json = null;
        try {
        	menuService.saveOrUpdate(menu);
        	json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
        return json;  
    }   
	
	@ResponseBody  
	@RequestMapping(value="menu/{id}",method=RequestMethod.PUT)
	public Object update(HttpSession httpSession,@PathVariable("id") Integer id,WXMenu menu){  
		String json = null;
		try {
			menu.setId(id);
			menuService.saveOrUpdate(menu);
        	json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}

	
	/**
	 * 查询所有
	 * search
	 * 
	 * @param httpSession
	 * @param agentId
	 * @param query
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="menu",method=RequestMethod.GET)
	public Object search(HttpSession httpSession,Integer agentId,PageQuery query){  
		String json = null;
		try {
			json = menuService.findToTree(agentId);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	
	/**
	 * 获取单个
	 * @param operationWork
	 * @param start
	 * @param limit
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="menu/{id}",method=RequestMethod.GET)
	public Object get(@PathVariable("id") Integer id){  
		String json = null;
		try {
			json = menuService.findByIdToJson(id);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}  
	
	/**
	 * 删除
	 * delete
	 * 
	 * @param id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="menu/{id}",method=RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Integer id){  
		String json = null;
		try {
			menuService.delete(id);
			json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		
		return json;  
	}
	
	/**
	 * 获取所有有效父菜单
	 * combo
	 * getMenuToCombo
	 * 
	 * @param agentId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="menu/combo/{agentId}",method=RequestMethod.GET)
	public Object getMenuToCombo(@PathVariable("agentId") Integer agentId){  
		String json = null;
		try {
			json = menuService.findParentMenus(agentId);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		
		return json;  
	}  
	
	/**
	 * 更新应用菜单到微信
	 * updateMemuToWX
	 * 
	 * @param agentId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="menu/wx/{agentId}",method=RequestMethod.PUT)
	public Object updateMemuToWX(@PathVariable("agentId") Integer agentId){  
		String json = null;
		try {
			menuService.updateMemuToWX(agentId);
			json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		
		return json;  
	}
 
}
